package thread;

public class Demo6 {
    public static void main(String[] args) {
        //lambda表达式()->{}
        Thread t1 = new Thread(() -> {
            while (true) {
                System.out.println("Hello t1!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t1");
        t1.setDaemon(true);
        t1.start();

        Thread t2 = new Thread(() -> {
            while (true) {
                System.out.println("Hello t2!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t2");
        t2.setDaemon(true);
        t2.start();

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    System.out.println("hello main!");
                    // 这里的三次打印不一定会全部执行，因为t线程被设置为后台线程，
                    // 当main方法也就是main线程（前台线程）结束后，后台线程无法阻挡进程的结束，所以打印次数不一定是三次
                }
            }
        };

        Thread t = new Thread(runnable);
        //将t设置为后台进程
        t.setDaemon(true);
        t.start();
    }
}
